from django.shortcuts import *
from demo.models import *
# Create your views here.
def show(request):
    proInfo_list = proInfo.objects.all()
    protype_list = protype.objects.all()
    if request.method == "GET":
        return render(request, "show.html", {
            "proInfo_list": proInfo_list,
            "protype_list": protype_list
        })
    else:
        type = request.POST.get("type")
        name = request.POST.get("name")
        if type == "全部":
            gg = proInfo.objects.filter(name__contains=name)
        elif name == "":
            gg = proInfo.objects.filter(type=type)
        else:
            gg = proInfo.objects.filter(name__contains=name,type=type).all()
        return render(request, "show.html", {
            "protype_list": protype_list,
            "gg":gg
        })
        # else:
        #     proInfo_list = proInfo.objects.filter(type__name__contains=type).filter(name__contains=name)
        #     print(proInfo_list)
        #     return render(request, "show.html", {
        #         "proInfo_list": proInfo_list,
        #         "protype_list": protype_list
        #     })
def insert(request):
    protype_list = protype.objects.all()
    if request.method == "GET":
        return render(request,"insert.html",{
            "protype_list":protype_list
        })
    else:
        name = request.POST.get("name")
        type = request.POST.get("type")
        addTime = request.POST.get("addTime")
        unit = request.POST.get("unit")
        price = request.POST.get("price")
        description = request.POST.get("description")
        proInfo.objects.create(name=name,type_id=type,addTime=addTime,unit=unit,price=price,description=description)
        return redirect("show")


def update(request,id):
    proInfo_d = proInfo.objects.filter(id=id).first()
    proInfo_d.isdestroy = 1
    proInfo_d.save()
    return redirect("show")